
package beans;

import java.util.*;
import java.sql.*;
import java.io.*;

/** Create list of components in DB
 * 
 * @param hddList - list of hhds
 * @param  motherboardList - list of motherboards
 * @param  processorList - list of processors
 * @param ramList - list of rams
 * @param videocardList - list of videocards
 */

public class ComponentListBean {
    
   // private Collection componentList;
    private Collection hddList;
    private Collection motherboardList;
    private Collection processorList;
    private Collection ramList;
    private Collection videocardList;
    private String url=null;

    // this constructor is not really used in the application
    // but is here for testing purpose

    public ComponentListBean() throws Exception{
      this(
          "jdbc:mysql://localhost/computershop?user=root&password=glurk");
    }
    
    /** Creates a new instance of ComponentListBean */

    public ComponentListBean(String _url) throws Exception {
        url=_url;
        Connection conn =null;
        Statement stmt = null;
        ResultSet rs = null;
        //componentList = new ArrayList();    // a list
        hddList = new ArrayList();
        motherboardList = new ArrayList();
        processorList = new ArrayList();
        ramList = new ArrayList();
        videocardList = new ArrayList();
        
        try{
            
	    // get a database connection and load the JDBC-driver

            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection(url);
            
	    // create SQL statements to load the components into the list
	    // each componet is a HDDBean,
            //                    MotherBoardBean,
            //                    ProcessorBean,
            //                    RAMBean,
            //                    VideocardBean
            //                                   object

            stmt = conn.createStatement();
        
            String sql_hdd = "SELECT ID_HDD, NAME_HDD, PRICE, COUNT,";
	    sql_hdd += "MANUFACTURER_NAME AS HDDMANNAME ";
            sql_hdd += "FROM HDD,";
	    sql_hdd += "MANUFACTURER WHERE HDD.ID_MAN=MANUFACTURER.ID_MANUFACTURER";
            rs= stmt.executeQuery(sql_hdd);
            
	    // analyze the result set

            while(rs.next()){
                
                // Save data to the HDDBean
                HDDBean hddBean = new HDDBean();
                
                hddBean.setId(rs.getInt("ID_HDD"));
                hddBean.setName(rs.getString("NAME_HDD"));
                hddBean.setPrice(rs.getInt("PRICE"));
                hddBean.setManufacturerName(rs.getString("HDDMANNAME"));
                hddBean.setCount(rs.getInt("COUNT"));
                
                hddList.add(hddBean);
                
            }
            
            String sql_motherboard = "SELECT ID_MB, NAME_MB, PRICE, COUNT,";
	    sql_motherboard += "MANUFACTURER_NAME AS MBMANNAME ";
            sql_motherboard += "FROM MOTHERBOARD,";
	    sql_motherboard += "MANUFACTURER WHERE MOTHERBOARD.ID_MAN=MANUFACTURER.ID_MANUFACTURER";
            rs= stmt.executeQuery(sql_motherboard);
            
            while(rs.next()){
                
                // Save data to the MotherboardBean
                MotherboardBean motherboardBean = new MotherboardBean();
                
                motherboardBean.setId(rs.getInt("ID_MB"));
                motherboardBean.setName(rs.getString("NAME_MB"));
                motherboardBean.setPrice(rs.getInt("PRICE"));
                motherboardBean.setManufacturerName(rs.getString("MBMANNAME"));
                motherboardBean.setCount(rs.getInt("COUNT"));
                
                motherboardList.add(motherboardBean);
                
            }   
            
            String sql_processor = "SELECT ID_PROC, NAME_PROC, PRICE, COUNT,";
	    sql_processor += "MANUFACTURER_NAME AS PROCMANNAME ";
            sql_processor += "FROM PROCESSOR,";
	    sql_processor += "MANUFACTURER WHERE PROCESSOR.ID_MAN=MANUFACTURER.ID_MANUFACTURER";
            rs= stmt.executeQuery(sql_processor);
            
            while(rs.next()){
                
                // Save data to the ProcessorBean
                ProcessorBean processorBean = new ProcessorBean();
                
                processorBean.setId(rs.getInt("ID_PROC"));
                processorBean.setName(rs.getString("NAME_PROC"));
                processorBean.setPrice(rs.getInt("PRICE"));
                processorBean.setManufacturerName(rs.getString("PROCMANNAME"));
                processorBean.setCount(rs.getInt("COUNT"));
                
                processorList.add(processorBean);
                
            }    
            
            String sql_RAM = "SELECT ID_RAM, NAME_RAM, PRICE, COUNT,";
	    sql_RAM += "MANUFACTURER_NAME AS RAMMANNAME ";
            sql_RAM += "FROM RAM,";
	    sql_RAM += "MANUFACTURER WHERE RAM.ID_MAN=MANUFACTURER.ID_MANUFACTURER";
            rs= stmt.executeQuery(sql_RAM);
            
            while(rs.next()){
                
                // Save data to the RAMBean
                RAMBean ramBean = new RAMBean();
                
                ramBean.setId(rs.getInt("ID_RAM"));
                ramBean.setName(rs.getString("NAME_RAM"));
                ramBean.setPrice(rs.getInt("PRICE"));
                ramBean.setManufacturerName(rs.getString("RAMMANNAME"));
                ramBean.setCount(rs.getInt("COUNT"));
                
                ramList.add(ramBean);
                
            }
            
            String sql_videocard = "SELECT ID_VC, NAME_VC, PRICE, COUNT,";
	    sql_videocard += "MANUFACTURER_NAME AS VCMANNAME ";
            sql_videocard += "FROM VIDEOCARD,";
	    sql_videocard += "MANUFACTURER WHERE VIDEOCARD.ID_MAN=MANUFACTURER.ID_MANUFACTURER";
            rs= stmt.executeQuery(sql_videocard);
            
            while(rs.next()){
                
                // Save data to the VideocardBean
                VideocardBean videocardBean = new VideocardBean();
                
                videocardBean.setId(rs.getInt("ID_VC"));
                videocardBean.setName(rs.getString("NAME_VC"));
                videocardBean.setPrice(rs.getInt("PRICE"));
                videocardBean.setManufacturerName(rs.getString("VCMANNAME"));
                videocardBean.setCount(rs.getInt("COUNT"));
                
                videocardList.add(videocardBean);
            
            }
        
        }
        catch(SQLException sqle){
            throw new Exception(sqle);
        }

	// note the we always try to close all services
	// even if one or more fail to close
	
        finally{
 	    try{
              rs.close();
            }
            catch(Exception e) {}
            try{
              stmt.close();
            }
	    catch(Exception e) {}
            try {
              conn.close();
            }
            catch(Exception e){}
        }
    }
    
    // return the computerlist
    
   // java.util.Collection getProduktLista() {
     //   return componentList;
    //}
    
    /** create an XML document from the componentlist
     * 
     * @return XML document with a list of components
     */

    public String getXml() {
        
        HDDBean hddBean=null;
        MotherboardBean motherboardBean=null;
        ProcessorBean processorBean=null;
        RAMBean ramBean=null;
        VideocardBean videocardBean=null;
        //Iterator iter = componentList.iterator();
        Iterator iter_hdd = hddList.iterator();
        Iterator iter_motherboard = motherboardList.iterator();
        Iterator iter_processor = processorList.iterator();
        Iterator iter_ram = ramList.iterator();
        Iterator iter_videocard = videocardList.iterator();
        StringBuffer buff = new StringBuffer();
        
        buff.append("<componentlist>");
        buff.append("<hardDrives>");
        // Add all hard drives to the components list
        while(iter_hdd.hasNext()){
            hddBean=(HDDBean)iter_hdd.next();
            buff.append(hddBean.getXml());
        }
        buff.append("</hardDrives>");
        buff.append("<motherboards>");
        // Add all motherboards to the components list
        while(iter_motherboard.hasNext()){
            motherboardBean=(MotherboardBean)iter_motherboard.next();
            buff.append(motherboardBean.getXml());
        }
        buff.append("</motherboards>");
        buff.append("<processors>");
        // Add all processors to the components list
        while(iter_processor.hasNext()){
            processorBean=(ProcessorBean)iter_processor.next();
            buff.append(processorBean.getXml());
        }
        buff.append("</processors>");
        buff.append("<rams>");
        // Add all RAMs to the components list
        while(iter_ram.hasNext()){
            ramBean=(RAMBean)iter_ram.next();
            buff.append(ramBean.getXml());
        }
        buff.append("</rams>");
        buff.append("<videocards>");
        // Add all videocards to the components list
        while(iter_videocard.hasNext()){
            videocardBean=(VideocardBean)iter_videocard.next();
            buff.append(videocardBean.getXml());
        }
        buff.append("</videocards>");
        buff.append("</componentlist>");        
        
        return buff.toString();
    }
    

    /** Search for a hdd by ID - used in ShopServelt.java
     * 
     * @param id - id of seeking hdd
     * @return - founded hdd, otherwise - null
     */

    public HDDBean getHDDById(int id) {
	HDDBean hddBean = null;
	Iterator iter = hddList.iterator();
        
	while(iter.hasNext()){
	    hddBean=(HDDBean)iter.next();
	    if(hddBean.getId()== id){
                return hddBean;
	    }
	}
	return null;
    }
    
     /** Search for a motherboard by ID - used in ShopServelt.java
     * 
     * @param id - id of seeking motherboard
     * @return - founded motherboard, otherwise - null
     */
    public MotherboardBean getMotherboardById(int id) {
	MotherboardBean motherboardBean = null;
	Iterator iter = motherboardList.iterator();
        
	while(iter.hasNext()){
	    motherboardBean=(MotherboardBean)iter.next();
	    if(motherboardBean.getId()== id){
                return motherboardBean;
	    }
	}
	return null;
    }
    
     /** Search for a processor by ID - used in ShopServelt.java
     * 
     * @param id - id of seeking processor
     * @return - founded processor, otherwise - null
     */
    public ProcessorBean getProcessorById(int id) {
	ProcessorBean processorBean = null;
	Iterator iter = processorList.iterator();
        
	while(iter.hasNext()){
	    processorBean=(ProcessorBean)iter.next();
	    if(processorBean.getId()== id){
                return processorBean;
	    }
	}
	return null;
    }
    
     /** Search for a ram by ID - used in ShopServelt.java
     * 
     * @param id - id of seeking ram
     * @return - founded ram, otherwise - null
     */
    public RAMBean getRAMById(int id) {
	RAMBean ramBean = null;
	Iterator iter = ramList.iterator();
        
	while(iter.hasNext()){
	    ramBean=(RAMBean)iter.next();
	    if(ramBean.getId()== id){
                return ramBean;
	    }
	}
	return null;
    }
    
     /** Search for a videocard by ID - used in ShopServelt.java
     * 
     * @param id - id of seeking videocard
     * @return - founded videocard, otherwise - null
     */
    public VideocardBean getVideocardById(int id) {
	VideocardBean videocardBean = null;
	Iterator iter = videocardList.iterator();
        
	while(iter.hasNext()){
	    videocardBean=(VideocardBean)iter.next();
	    if(videocardBean.getId()== id){
                return videocardBean;
	    }
	}
	return null;
    }
    
    /** A main procedure used for testing, remember that a bean can be run
    * without a container
    */

    public static void main(String[] args){
        try{
	    ComponentListBean blb = new ComponentListBean();
	    System.out.println(blb.getXml());
        }
        catch(Exception e){
	    System.out.println(e.getMessage());
        }
    }
}

